home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Creator Changer 2.7 / Code & Resource / Creator Changer.AE.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-21  |  7.1 KB  |  238 lines  |  [TEXT/KAHL]

  1. /**********************************************************************
  2.  *    This file contains the functions which initialize the core 
  3.  *    AppleEvents for the program.
  4.  **********************************************************************/
  5.  
  6. #include    "Creator Changer.h"
  7. #include    "Creator Changer.AE.h"
  8. #include    "GetFileIcon.h"
  9.  
  10.  
  11. /**********************************************************************
  12.  *    Function Install_AE_Handlers(), this function sets up the 
  13.  *    AppleEvents for the program.
  14.  **********************************************************************/
  15.  
  16. void Install_AE_Handlers(void)
  17.     {
  18.     
  19.     AEEventHandlerUPP open_ae, quit_ae, start_ae, print_ae;
  20.     
  21.     open_ae=NewAEEventHandlerProc(Open_AE);
  22.     quit_ae=NewAEEventHandlerProc(Quit_AE);
  23.     start_ae=NewAEEventHandlerProc(Start_AE);
  24.     print_ae=NewAEEventHandlerProc(Print_AE);
  25.     
  26.     AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,   open_ae,  FALSE, FALSE);
  27.     AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, quit_ae,  FALSE, FALSE);
  28.     AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, start_ae, FALSE, FALSE);
  29.     AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,  print_ae, FALSE, FALSE);
  30.     
  31.     }
  32.  
  33.  
  34.  
  35. /**********************************************************************
  36.  *    Function Open_AE(), this function supports what happens when a file
  37.  *    is dropped on Creator Changer.
  38.  **********************************************************************/
  39.  
  40. pascal OSErr Open_AE(AppleEvent *the_event, AppleEvent *the_reply, long ref_con)
  41.     {
  42.     
  43.     AEDescList            the_files;
  44.     long                num_of_files;
  45.         
  46.     AEGetParamDesc(the_event, keyDirectObject, typeAEList, &the_files);
  47.     AECountItems(&the_files, &num_of_files);
  48.     Done_With_Dialog=NO;
  49.     
  50.     if(!Dialog_Opened)
  51.         {
  52.         if((**Global).AutoChange && Option_Key_Down) Open_Auto_Change_Files(the_files, num_of_files);
  53.         else Open_Normal_Files(the_files, num_of_files);
  54.         if((**Global).DragQuit==TRUE) All_Done=TRUE;
  55.         }
  56.     else Do_Error(Multi_File_1, EMPTY_STR, Multi_File_2, Multi_File_3);
  57.     
  58.     Maintain_Menu_Items(FrontWindow());
  59.     return(noErr);
  60.     
  61.     }
  62.  
  63.  
  64.  
  65. /**********************************************************************
  66.  *    Function Open_Auto_Change_Files(), this function opens the files which are 
  67.  *    dropped on CC and when the Automatic change box is checked.  If there are files 
  68.  *    which are not specified in the types listing, then CC will open them with the 
  69.  *    normal Change Dialog.  I do this by starting another Event Loop, until
  70.  *    the user closes out the Dialog.
  71.  **********************************************************************/
  72.  
  73. void Open_Auto_Change_Files(AEDescList the_files, long num_of_files)
  74.     {
  75.     
  76.     AEKeyword            key_word;
  77.     DescType            the_type;
  78.     Size                the_size;
  79.     auto_chng            **rsrc_handle;
  80.     Str4                types[100];
  81.     short                 i, the_item=1;
  82.  
  83.     
  84.     Make_Pop_Up_Menu(Auto.menu_id, PREF_AC, Auto.num_items);
  85.     Get_Types_From_Prefs(types);
  86.     for(i=1;i<=num_of_files;i++)
  87.         {
  88.         Multiple_Files=NO;
  89.         AEGetNthPtr(&the_files, i, typeFSS, &key_word, &the_type, &The_File_Spec, sizeof(The_File_Spec), &the_size);
  90.         FSpGetFInfo(&The_File_Spec, &File_Info);
  91.         the_item=Check_File_Type(types, File_Info.fdType);
  92.         if(the_item)
  93.             {
  94.             UseResFile(CreatorChangerPref);
  95.                 rsrc_handle=(auto_chng **)GetResource(PREF_AC, The_ac_Type[the_item]);
  96.                 Set_Type_Long((**rsrc_handle).ToFile, &File_Info.fdType, NIL_PTR, NIL_PTR);
  97.                 Set_Type_Long((**rsrc_handle).ToCreator, &File_Info.fdCreator, NIL_PTR, NIL_PTR);
  98.                 ReleaseResource((Handle)rsrc_handle);
  99.             UseResFile(CreatorChangerApp);
  100.             FSpSetFInfo(&The_File_Spec, &File_Info);
  101.             Force_Finder_Update(&The_File_Spec);
  102.             }
  103.         else
  104.             {
  105.             GetFileIcon(&The_File_Spec, svAllAvailableData, &The_Icons);
  106.             Chng.files=the_files;
  107.             Chng.num_files=one_file;
  108.             Open_DLOG(&Chng);
  109.             while(!Done_With_Dialog) Handle_One_Event();
  110.             }
  111.         }
  112.     AEDisposeDesc(&the_files);
  113.     
  114.     }
  115.  
  116.  
  117.  
  118. /**********************************************************************
  119.  *    Function Open_Normal_Files(), this function this function opens the files if 
  120.  *    the Automatic Change box was NOT checked.  Once again I start another Event
  121.  *    Loop until the user closes the Change Dialog.
  122.  **********************************************************************/
  123.  
  124. void Open_Normal_Files(AEDescList the_files, long num_of_files)
  125.     {
  126.     
  127.     AEKeyword            key_word;
  128.     DescType            the_type;
  129.     Size                the_size;
  130.     
  131.     if(num_of_files==1)
  132.         {
  133.         Multiple_Files=NO;
  134.         AEGetNthPtr(&the_files, 1, typeFSS, &key_word, &the_type, &The_File_Spec, sizeof(The_File_Spec), &the_size);
  135.         FSpGetFInfo(&The_File_Spec, &File_Info);
  136.         GetFileIcon(&The_File_Spec, svAllAvailableData, &The_Icons);
  137.         AEDisposeDesc(&the_files);
  138.         }
  139.     else Multiple_Files=YES;
  140.     
  141.     Chng.files=the_files;
  142.     Chng.num_files=num_of_files;
  143.     Open_DLOG(&Chng);
  144.     while(!Done_With_Dialog) Handle_One_Event();
  145.     
  146.     Multiple_Files=NO;
  147.     
  148.     }
  149.  
  150.  
  151.  
  152. /**********************************************************************
  153.  *    Function Get_Types_From_Prefs(), this function gets the types from
  154.  *    preference file and stores them in an array, for later use.
  155.  **********************************************************************/
  156.  
  157. void Get_Types_From_Prefs(Str4 *the_array)
  158.     {
  159.     
  160.     short             i;
  161.     auto_chng        **the_handle;
  162.     
  163.     the_handle=(auto_chng **)NewHandle(sizeof(auto_chng));
  164.     UseResFile(CreatorChangerPref);
  165.         for(i=1;i<=Auto.num_items;i++)
  166.             {
  167.             the_handle=(auto_chng **)GetResource(PREF_AC, The_ac_Type[i]);
  168.             p_Str_Copy((**the_handle).FromFile, the_array[i]);
  169.             ReleaseResource((Handle)the_handle);
  170.             }
  171.     UseResFile(CreatorChangerApp);
  172.     
  173.     }
  174.  
  175.  
  176.  
  177. /**********************************************************************
  178.  *    Function Check_File_Type(), this function checks the current file's
  179.  *    file type against those in the types list, if the type is not there then it
  180.  *    returns BAD, else it returns the menu index at which it found the type.
  181.  **********************************************************************/
  182.  
  183. short Check_File_Type(Str4 *the_arry, OSType the_ostype)
  184.     {
  185.     
  186.     short         i;
  187.     File_Union    the_union_type;
  188.     
  189.     Set_Type_Text(&the_union_type, the_ostype);
  190.     
  191.     for(i=1;i<=Auto.num_items;i++) if(Comp_The_String(the_arry[i], the_union_type.TEXT)) return(i);
  192.     return(BAD);
  193.     
  194.     }
  195.  
  196.  
  197.  
  198. /**********************************************************************
  199.  *    Function Quit_AE(), this function supports the 'quit' event.  All
  200.  *    that it does is set the global switch to true to quit.
  201.  **********************************************************************/
  202.  
  203. pascal OSErr Quit_AE(AppleEvent *theEvent, AppleEvent *reply, long ref_con)
  204.     {
  205.     
  206.     All_Done=TRUE;
  207.     return(noErr);
  208.     
  209.     }
  210.  
  211.  
  212.  
  213. /**********************************************************************
  214.  *    Function Print_AE(), this function does nothing, besides report 
  215.  *    that CC does not support the print event.
  216.  **********************************************************************/
  217.  
  218. pascal OSErr Print_AE(AppleEvent *theEvent, AppleEvent *reply, long ref_con)
  219.     {
  220.     
  221.     return(errAEEventNotHandled);
  222.     
  223.     }
  224.  
  225.  
  226.  
  227. /**********************************************************************
  228.  *    Function Start_AE(), this function controls what happens when 
  229.  *    Creator Changer is launched.
  230.  **********************************************************************/
  231.  
  232. pascal OSErr Start_AE(AppleEvent *theEvent, AppleEvent *reply, long ref_con)
  233.     {
  234.     
  235.     Handle_File_Choice(F_OPEN_ITEM);
  236.     return(noErr);
  237.         
  238.     }